package com.ylc.jwtdemo.controller;

import com.ylc.jwtdemo.entity.Userme;
import com.ylc.jwtdemo.service.UsermeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/userme")
public class UsermeController {
    @Autowired
    private UsermeService usermeService;

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Integer delete(Integer usermeId) {
        System.out.println(usermeId);
        int result = usermeService.delete(usermeId);
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(Userme userme) {
        int result = usermeService.Update(userme);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Userme insert(Userme userme) {
        return usermeService.insertUserme(userme);
    }

    @RequestMapping("/ListUser")
    @ResponseBody
    public List<Userme> ListUserme() {
        return usermeService.ListUserme();
    }

    @RequestMapping("/ListByName")
    @ResponseBody
    public List<Userme> ListUsermeByName(String usermeName) {
        return usermeService.findUsermeByName(usermeName);
    }

    /**
     * 分页
     * @return
     */
    @RequestMapping(value="/page")
    @ResponseBody
    public List<Userme> page(Integer page){
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRows = pageSize*(pageNow-1);
        List<Userme> list = usermeService.queryPage(startRows);
        return list;
    }

    /**
     * rows
     * @return
     */
    @RequestMapping(value="/rows")
    @ResponseBody
    public int rows(){
        return usermeService.getRowCount();
    }

}
